package com.zzedu.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzedu.annon.AccessPermission;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.enums.OperatorType;
import com.zzedu.common.enums.UserTypeEnumns;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.domain.ZzStuNavicateLog;
import com.zzedu.enmuns.ConfigEnums;
import com.zzedu.result.Result;
import com.zzedu.result.ResultEnum;
import com.zzedu.service.IZzStuNavicateLogService;
import com.zzedu.system.service.ISysConfigService;
import com.zzedu.utils.BaseReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 导航
 */
@RestController
@RequestMapping("/nav")
@AccessPermission(value = {UserTypeEnumns.STUDENT})
public class StuNavicateLogController {


    @Autowired
    IZzStuNavicateLogService zzStuNavicateLogService;

    @Autowired
    ISysConfigService sysConfigService;

    /**
     * 学生-导航视频
     * @param baseReq
     * @return
     */
    @PostMapping(value = {"/query"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "学生-导航视频", businessType = BusinessType.APP_STUDENT, operatorType = OperatorType.MOBILE)
    public Result query(@Validated @RequestBody BaseReq baseReq) {
        Result result = new Result();
        String url = sysConfigService.selectConfigByKeyCache(ConfigEnums.SYS_NAVIGATE_VIDEO_URL.getCode());
        JSONObject obj = new JSONObject();
        obj.put("url", url);
        obj.put("readFlag", null != zzStuNavicateLogService.getOne(Wrappers.lambdaQuery(ZzStuNavicateLog.class)
                .eq(ZzStuNavicateLog::getStuId, SecurityUtils.getUserId()).last("limit 1")
        ));
        result.setData(obj);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }

    /**
     * 学生-导航阅读-记录
     * @param baseReq
     * @return
     */
    @PostMapping(value = {"/save"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "学生-导航阅读-记录", businessType = BusinessType.APP_STUDENT, operatorType = OperatorType.MOBILE)
    public Result save(@Validated @RequestBody BaseReq baseReq) {
        Result result = new Result();
        ZzStuNavicateLog zzStuNavicateLog = new ZzStuNavicateLog();
        zzStuNavicateLog.setStuId(SecurityUtils.getUserId());
        zzStuNavicateLogService.save(zzStuNavicateLog);
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        return result;
    }
}
